﻿using App.Dal;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.Pails
{
    public partial class FrmList : UIPage
    {
        int CurrentRowIndex = -1;
        private QueryPageModel queryModel;
        public QueryPageModel QueryModel
        {
            get
            {
                if (queryModel == null)
                {
                    queryModel = new QueryPageModel();
                }
                return queryModel;
            }
            set
            {
                queryModel = value;
            }
        }

        public FrmList()
        {
            InitializeComponent();
            dataGridView.AutoGenerateColumns = false;
            dataGridView.ReadOnly = true;
            dataGridView.MultiSelect = false;
            dataGridView.RowHeadersVisible = false;
            dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            GetList();
        }

        private void FrmSamplingData_Resize(object sender, EventArgs e)
        {
        }

        public void GetList()
        {
            var data = SysPailServices.GetAllPageList();
            foreach (var item in data)
            {
                item.ShowDisable = item.Disable == 1 ? "禁用" : "启用";
            }
            dataGridView.DataSource = data;
            CurrentRowIndex = -1;
        }

        private void dataGridView_SelectIndexChange(object sender, int index)
        {
            CurrentRowIndex = index;
        }

        private void FrmSamplingData_Load(object sender, EventArgs e)
        {
            GetList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmEdit frmEdit = new FrmEdit();
            frmEdit.ShowDialog();
            GetList();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex < 0)
            {
                UIMessageBox.ShowWarning("请选择一行");
                return;
            }
            var id = dataGridView.Rows[CurrentRowIndex].Cells["Id"].Value.ToIntEx();
            FrmEdit edit = new FrmEdit(id);
            edit.ShowDialog();
            GetList();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex < 0)
            {
                UIMessageBox.ShowWarning("请选择一行");
                return;
            }
            var id = dataGridView.Rows[CurrentRowIndex].Cells["Id"].Value.ToIntEx();
            var name = dataGridView.Rows[CurrentRowIndex].Cells["PailName"].Value.ToStringEx();
            if (UIMessageBox.ShowAsk($"确认删除 [ {name} ]？"))
            {
                if (SysPailServices.Delete(id))
                {
                    GetList();
                    UIMessageBox.Show("删除成功");
                }
                else
                    UIMessageBox.ShowError("删除失败");
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            if (CurrentRowIndex < 0)
            {
                UIMessageBox.ShowWarning("请选择一行");
                return;
            }
            var id = dataGridView.Rows[CurrentRowIndex].Cells["Id"].Value.ToIntEx();
            var name = dataGridView.Rows[CurrentRowIndex].Cells["PailName"].Value.ToStringEx();
            if (UIMessageBox.ShowAsk($"确认复位桶： [ {name} ]的记录？"))
            {
                if (SysPailServices.Resetting(id))
                {
                    GetList();
                    UIMessageBox.Show("复位成功");
                }
                else
                    UIMessageBox.ShowError("操作失败");
            }
            GetList();
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
               
             
        }

        private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CurrentRowIndex = e.RowIndex;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (UIMessageBox.ShowAsk($"确认复位所有桶重量/点位信息？"))
            {
                if (SysPailServices.ResettingAll())
                {
                    GetList();
                    UIMessageBox.Show("复位成功");
                }
                else
                    UIMessageBox.ShowError("操作失败");
            }
            GetList();
        }
    }
}
